#nullable enable
using Godot;
using System.Collections.Generic;
using AndroidUnevenRoadGame.Game;

namespace AndroidUnevenRoadGame.UI
{
    public partial class ShopScene : Control
    {
        [Export] private Label? _goldLabel;
        [Export] private ItemList? _itemList;
        [Export] private Button? _buyButton;
        [Export] private Button? _leaveButton;
        [Export] private Label? _messageLabel;

        private readonly List<GameRoot.ShopOffer> _offers = new();

        public override void _Ready()
        {
            _goldLabel ??= GetNode<Label>("Panel/VBox/GoldLabel");
            _itemList ??= GetNode<ItemList>("Panel/VBox/ItemList");
            _messageLabel ??= GetNode<Label>("Panel/VBox/MessageLabel");
            _buyButton ??= GetNode<Button>("Panel/VBox/ButtonRow/BuyButton");
            _leaveButton ??= GetNode<Button>("Panel/VBox/ButtonRow/LeaveButton");

            _itemList!.ItemSelected += OnItemSelected;
            _itemList.ItemActivated += OnItemActivated;
            _buyButton!.Pressed += OnBuyPressed;
            _leaveButton!.Pressed += OnLeavePressed;

            Refresh();
        }

        private void Refresh()
        {
            var game = GameRoot.Instance;
            if (game.CurrentShopOffers.Count == 0)
            {
                game.GenerateShopOffers();
            }

            _offers.Clear();
            _offers.AddRange(game.CurrentShopOffers);

            _itemList!.Clear();
            for (var i = 0; i < _offers.Count; i++)
            {
                var offer = _offers[i];
                var card = CardDatabase.Get(offer.CardId);
                _itemList.AddItem($"{card.Name} - {offer.Price} 金币");
                _itemList.SetItemTooltip(i, card.Description);
            }

            UpdateGoldLabel();
            UpdateMessage(string.Empty);
            UpdateBuyButtonState();
        }

        private void UpdateGoldLabel()
        {
            _goldLabel!.Text = $"金币：{GameRoot.Instance.PlayerGold}";
        }

        private void UpdateBuyButtonState()
        {
            _buyButton!.Disabled = _itemList!.GetSelectedItems().Length == 0;
        }

        private void UpdateMessage(string text)
        {
            _messageLabel!.Text = text;
        }

        private void OnItemSelected(long index)
        {
            UpdateBuyButtonState();
        }

        private void OnItemActivated(long index)
        {
            _itemList!.Select((int)index);
            OnBuyPressed();
        }

        private void OnBuyPressed()
        {
            var selected = _itemList!.GetSelectedItems();
            if (selected.Length == 0)
            {
                UpdateMessage("请先选择一件商品。");
                return;
            }

            var offerIndex = selected[0];
            if (offerIndex < 0 || offerIndex >= _offers.Count)
            {
                UpdateMessage("选择无效。");
                return;
            }

            var offer = _offers[offerIndex];
            var card = CardDatabase.Get(offer.CardId);
            var game = GameRoot.Instance;

            if (!game.TryPurchase(offer))
            {
                UpdateMessage("金币不足，无法购买。");
                return;
            }

            UpdateMessage($"获得卡牌：{card.Name}");
            Refresh();
        }

        private void OnLeavePressed()
        {
            var game = GameRoot.Instance;
            game.AdvanceFloor();
            game.ClearShopOffers();
            game.ChangeState(GameRoot.GameState.Map);
        }
    }
}

